<template>
	<div class="app-container">
		<!-- 查询和其他操作 -->
		<div class="filter-container">
			<el-input v-model="listQuery.userNickName" clearable style="width: 180px" class="filter-item"
			          placeholder="用户昵称"/>
			<el-input v-model="listQuery.problemDescription" clearable style="width: 180px" class="filter-item"
			          placeholder="反馈内容"/>
			<el-select v-model="listQuery.type" style="width: 180px" class="filter-item" placeholder="请选择类型">
				<el-option v-for="(key, value) in typeMap" :key="key" :label="key" :value="value"/>
			</el-select>
			<el-button v-permission="[1]" clearable class="filter-item" type="primary" icon="el-icon-search"
			           @click="handleFilter">查找
			</el-button>
		</div>
		
		<!-- 查询结果 -->
		<el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" border fit
		          highlight-current-row>
			
			<el-table-column align="center" width="100" label="操作" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button v-permission="[1]" v-if="scope.row.status != 1" type="primary" size="mini"
					           @click="handleUpdate(scope.row)">修改
					</el-button>
				</template>
			</el-table-column>
			<el-table-column align="center" width="80" label="反馈编号" prop="id"/>
			
			<el-table-column align="center" label="问题类型" prop="problemType"/>
			<el-table-column align="center" label="问题描述" prop="problemDescription"/>
			<el-table-column align="center" width="100" label="创建人" prop="user.nickName"/>
			<el-table-column align="center" width="120" label="联系方式" prop="phone"/>
			<el-table-column align="center" width="100" label="类型" prop="status">
				<template slot-scope="scope">
					<el-tag>{{ scope.row.status | feedbackTypeFilter }}</el-tag>
				</template>
			</el-table-column>
			<el-table-column align="center" width="120" label="问题截图" prop="problemPicture">
				<template slot-scope="scope">
					<el-image
							style="width: 100px; height: 100px; cursor: pointer"
							:fit="'cover'"
							:src="handleImgArray(scope.row.problemPicture)[0]"
							@click="showViewer(scope.row.problemPicture)">
					</el-image>
				</template>
			</el-table-column>
			
			<el-table-column align="center" label="评论时间" prop="createTime">
				<template slot-scope="scope">{{ scope.row.createTime | formatTime }}</template>
			</el-table-column>
		
		</el-table>
		
		<pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
		            @pagination="getList"/>
		
		<el-dialog :visible.sync="showImg" title="图片详情">
			<div v-for="(item, index) in url" :key="index">
				<img :src="item" width="100%">
			</div>
		</el-dialog>
	
	</div>
</template>

<script>
	import {listFeedback, updateFeedback} from '@/api/feedback'
	import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
	
	const typeMap = {
		0: '解决中',
		1: '已解决',
		2: '未解决'
	}
	
	export default {
		name: 'Feedback',
		components: {Pagination},
		filters: {
			feedbackTypeFilter(status) {
				return typeMap[status]
			}
		},
		data() {
			return {
				list: undefined,
				total: 0,
				listLoading: true,
				listQuery: {
					pageNum: 1,
					pageSize: 20,
					type: undefined,
					problemDescription: undefined,
					userNickName: undefined
				},
				url: [],
				showImg: false,
				typeMap
			}
		},
		created() {
			this.getList()
		},
		methods: {
			getList() {
				this.listLoading = true
				listFeedback(this.listQuery)
					.then(res => {
						this.list = res.data.pageList
						this.total = res.data.total
						this.listLoading = false
					})
					.catch(() => {
						this.list = []
						this.total = 0
						this.listLoading = false
					})
			},
			handleImgArray(arr) {
				return arr.split(',')
			},
			handleUpdate(row) {
				let data = {
					id: row.id,
					status: 1,
				}
				updateFeedback(data)
					.then(res => {
						this.$notify.success({
							title: '成功',
							message: '操作成功'
						})
						this.getList()
					})
					.catch(res => {
						this.$notify.error({
							title: '失败',
							message: res
						})
					})
			},
			showViewer(row) {
				this.url = this.handleImgArray(row)
				this.showImg = true
			},
			closeViewer() {
				this.showImg = false
			},
			handleFilter() {
				this.listQuery.pageNum = 1
				this.getList()
			}
		}
	}
</script>
